package cn.fl.riskctrl.jxldb.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.riskctrl.jxldb.service.IJxldbModifyService;
import cn.fl.riskctrl.jxldb.service.IJxldbSearchService;
import cn.fl.riskctrl.jxldb.vo.JxlmgReportVO;
import cn.fl.system.constant.SysLogConstant;
import fl.riskctrl.facade.jxldb.condition.JxlmgReportQueryCondition;

/**
 * @Description:  控制层
 */
@Controller
@RequestMapping("/riskctrl/jxlmgReport")
public class JxlmgReportController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(JxlmgReportController.class);

	/**
	 * jxlmgReport 服务层BEAN
	 */
	@Autowired
	private IJxldbSearchService jxlSearchService;
	@Autowired
	private IJxldbModifyService jxlModifyService;
	/**
	 * 到列表页面  
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("riskctrl/jxldb/jxlmgReportList");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("riskctrl/jxldb/jxlmgReportModify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		//四个查询条件
		String usrNm = request.getParameter("usrNm");
		String certNo = request.getParameter("certNo");
		String mp = request.getParameter("mp");
		String age = request.getParameter("age");
		String genderCd = request.getParameter("genderCd");
		
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		JxlmgReportQueryCondition qc = new JxlmgReportQueryCondition();
		if (StringUtils.isNotEmpty(usrNm)) {
			qc.setUsrNm(usrNm);
		}
		if (StringUtils.isNotEmpty(certNo)) {
			qc.setCertNo(certNo);
		}
		if (StringUtils.isNotEmpty(mp)) {
			qc.setMp(mp);
		}
		if (StringUtils.isNotEmpty(age)) {
			qc.setAge(Double.valueOf(age));
		}
		if (StringUtils.isNotEmpty(genderCd)) {
			qc.setGenderCd(genderCd);
		}
		qc.setIsDel(0); 
		/*qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());*/
		page.setCondition(qc);

		Pager pager = this.jxlSearchService.searchJxlMgReport(page);
		List<JxlmgReportVO> list = (List<JxlmgReportVO>) pager.getResultList();
		ListPageVO<JxlmgReportVO> listPage = new ListPageVO<JxlmgReportVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			JxlmgReportVO vo = getVOById(id);
			
			
			if(vo == null){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				return result;
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	private JxlmgReportVO getVOById(String id) {
		Pager page = new Pager();
		JxlmgReportQueryCondition qc = new JxlmgReportQueryCondition();
		qc.setId(Long.valueOf(id));
		page.setCondition(qc);
		Pager pager = this.jxlSearchService.searchJxlMgReport(page);
		List<JxlmgReportVO> resultList = (List<JxlmgReportVO>)pager.getResultList();
		if(CollectionUtils.isEmpty(resultList)){
			BaseLogUtils.error(logger, "getVOById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return resultList.get(0);
	}
	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String usrNm = request.getParameter("usrNm");
		String certNo = request.getParameter("certNo");
		String age = request.getParameter("age");
		String genderCd = request.getParameter("genderCd");
		String mp = request.getParameter("mp");
		String certIsValid = request.getParameter("certIsValid");
		String mpProvince = request.getParameter("mpProvince");
		String mpCity = request.getParameter("mpCity");
		String mpOperator = request.getParameter("mpOperator");
		String brnProvince = request.getParameter("brnProvince");
		String brnCity = request.getParameter("brnCity");
		String brnRegion = request.getParameter("brnRegion");
		String gryMp = request.getParameter("gryMp");
		String gryDrctCpBlkQty = request.getParameter("gryDrctCpBlkQty");
		String gryIndrctCpBlkQty = request.getParameter("gryIndrctCpBlkQty");
		String gryMpScore = request.getParameter("gryMpScore");
		String gryFoCpQty = request.getParameter("gryFoCpQty");
		String grySoCpBlkQty = request.getParameter("grySoCpBlkQty");
		String grySoCpBlkPct = request.getParameter("grySoCpBlkPct");
		String regMp = request.getParameter("regMp");
		String regMpQty = request.getParameter("regMpQty");
		String searchedOrgQty = request.getParameter("searchedOrgQty");
		String authOrg = request.getParameter("authOrg");
		String mgId = request.getParameter("mgId");
		String spendTm = request.getParameter("spendTm");

		String remark = request.getParameter("remark");
		String isValid = request.getParameter("isValid");
		String isDel = request.getParameter("isDel");
		String crtTm = request.getParameter("crtTm");
		String crtUsrId = request.getParameter("crtUsrId");
		String mdfTm = request.getParameter("mdfTm");
		String mdfUsrId = request.getParameter("mdfUsrId");
		
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		JxlmgReportVO entity = getVOById(id);
		try {
			if(entity == null){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				return result;
			}
			if(StringUtils.isNotEmpty(isDel)){
				entity.setIsDel(Integer.valueOf(isDel));
			}
			if (StringUtils.isNotEmpty(usrNm)) {
				entity.setUsrNm(usrNm);
			}
			if (StringUtils.isNotEmpty(certNo)) {
				entity.setCertNo(certNo);
			}
			if (StringUtils.isNotEmpty(age)) {
				entity.setAge(Double.valueOf(age));
			}
			if (StringUtils.isNotEmpty(genderCd)) {
				entity.setGenderCd(genderCd);
			}
			if (StringUtils.isNotEmpty(mp)) {
				entity.setMp(mp);
			}
			if (StringUtils.isNotEmpty(certIsValid)) {
				entity.setCertIsValid(Integer.valueOf(certIsValid));
			}
			if (StringUtils.isNotEmpty(mpProvince)) {
				entity.setMpProvince(mpProvince);
			}
			if (StringUtils.isNotEmpty(mpCity)) {
				entity.setMpCity(mpCity);
			}
			if (StringUtils.isNotEmpty(mpOperator)) {
				entity.setMpOperator(mpOperator);
			}
			if (StringUtils.isNotEmpty(brnProvince)) {
				entity.setBrnProvince(brnProvince);
			}
			if (StringUtils.isNotEmpty(brnCity)) {
				entity.setBrnCity(brnCity);
			}
			if (StringUtils.isNotEmpty(brnRegion)) {
				entity.setBrnRegion(brnRegion);
			}
			if (StringUtils.isNotEmpty(gryMp)) {
				entity.setGryMp(gryMp);
			}
			if (StringUtils.isNotEmpty(gryDrctCpBlkQty)) {
				entity.setGryDrctCpBlkQty(Integer.valueOf(gryDrctCpBlkQty));
			}
			if (StringUtils.isNotEmpty(gryIndrctCpBlkQty)) {
				entity.setGryIndrctCpBlkQty(Integer.valueOf(gryIndrctCpBlkQty));
			}
			if (StringUtils.isNotEmpty(gryMpScore)) {
				entity.setGryMpScore(Double.valueOf(gryMpScore));
			}
			if (StringUtils.isNotEmpty(gryFoCpQty)) {
				entity.setGryFoCpQty(Integer.valueOf(gryFoCpQty));
			}
			if (StringUtils.isNotEmpty(grySoCpBlkQty)) {
				entity.setGrySoCpBlkQty(Integer.valueOf(grySoCpBlkQty));
			}
			if (StringUtils.isNotEmpty(grySoCpBlkPct)) {
				entity.setGrySoCpBlkPct(grySoCpBlkPct);
			}
			if (StringUtils.isNotEmpty(regMp)) {
				entity.setRegMp(regMp);
			}
			if (StringUtils.isNotEmpty(regMpQty)) {
				entity.setRegMpQty(Integer.valueOf(regMpQty));
			}
			if (StringUtils.isNotEmpty(searchedOrgQty)) {
				entity.setSearchedOrgQty(Integer.valueOf(searchedOrgQty));
			}
			if (StringUtils.isNotEmpty(authOrg)) {
				entity.setAuthOrg(authOrg);
			}
			if (StringUtils.isNotEmpty(mgId)) {
				entity.setMgId(mgId);
			}
			if (StringUtils.isNotEmpty(spendTm)) {
				entity.setSpendTm(Double.valueOf(spendTm));
			}
			if (StringUtils.isNotEmpty(remark)) {
				entity.setRemark(remark);
			}
			if (StringUtils.isNotEmpty(isValid)) {
				entity.setIsValid(Integer.valueOf(isValid));
			}
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			boolean success = this.jxlModifyService.modifyJxlMgReport(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
}

